package cn.wk.interceptor;


import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Configuration
public class FeignInterceptor implements RequestInterceptor {
    private static final String TOKEN_NAME = "Authorization";

    @Override
    public void apply(RequestTemplate requestTemplate) {
        //获取请求头对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader(TOKEN_NAME);
        //把token添加到请求头
        requestTemplate.header(TOKEN_NAME,token);

    }
}
